﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 自定义扩展系统方法（马甲）
    /// </summary>
    public static class DateTimeHelper
    {
        /// <summary>
        /// 格式化日期 yyyy-MM-dd
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string Format(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return DateTimeHelper.Format(dt.Value, "yyyy-MM-dd");
            }
            return "";
        }

        /// <summary>
        /// 格式化日期 yyyy-MM-dd
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DateTimeFormat(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return DateTimeHelper.Format(dt.Value, "yyyy-MM-dd HH:mm");
            }
            return "";
        }

        /// <summary>
        /// 格式化日期 yyyy-MM-dd
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string Format(this DateTime dt)
        {
            return DateTimeHelper.Format(dt, "yyyy-MM-dd");
        }

        /// <summary>
        /// 格式化日期 yyyy-MM-dd
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DateTimeFormat(this DateTime dt)
        {
            return DateTimeHelper.Format(dt, "yyyy-MM-dd HH:mm");
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Format(this DateTime? dt, string format)
        {
            var str = "";
            if (dt != null && dt.HasValue)
            {
                str = dt.Value.ToString(format, DateTimeFormatInfo.InvariantInfo);
            }
            return str;
        }
        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Format(this DateTime dt, string format)
        {
            var str = "";
            if (dt != null)
            {
                str = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);
            }
            return str;
        }

        /// <summary>
        /// 年龄
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int Age(this DateTime dt)
        {
            return DateTimeHelper.Age(dt, DateTime.Now);
        }
        public static int Age(this DateTime dt, DateTime date)
        {
            int result = date.Year - dt.Year;
            if (!(date.Month > dt.Month || (date.Month == dt.Month && date.Day >= dt.Day)))
            {
                result -= 1;
            }
            return result;
        }
        /// <summary>
        /// 年龄
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int Age(this DateTime? dt)
        {
            return DateTimeHelper.Age(dt, DateTime.Now);
        }
        public static int Age(this DateTime? dt, DateTime date)
        {
            if (dt.HasValue)
            {
                int result = DateTime.Now.Year - dt.Value.Year;
                if (!(date.Month > dt.Value.Month || (date.Month == dt.Value.Month && date.Day >= dt.Value.Day)))
                {
                    result -= 1;
                }
                return result;
            }
            return 0;
        }


        #region 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// <summary>
        /// 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns></returns>
        public static string CaculateWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            string weekstr = "";
            switch (week)
            {
                case 1: weekstr = "星期一"; break;
                case 2: weekstr = "星期二"; break;
                case 3: weekstr = "星期三"; break;
                case 4: weekstr = "星期四"; break;
                case 5: weekstr = "星期五"; break;
                case 6: weekstr = "星期六"; break;
                case 7: weekstr = "星期日"; break;
            }

            return weekstr;
        }
        #endregion
        /// <summary>
        /// 获取星期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetWeek(this DateTime? dt)
        {
            string result = "";
            if (dt.HasValue)
            {
                result = CaculateWeekDay(dt.Value.Year, dt.Value.Month, dt.Value.Day);
            }
            return result;
        }
        /// <summary>
        /// 获取星期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetWeek(this DateTime dt)
        {
            return CaculateWeekDay(dt.Year, dt.Month, dt.Day);
        }

        /// <summary>
        /// 格式化Bool
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>        
        public static string Format(this bool? b)
        {
            var str = "";
            if (!b.HasValue)
            {
                str = "";
            }
            else if (b == true)
            {
                str = "✔";
            }
            else
            {
                str = "✘";
            }
            return str;
        }
        /// <summary>
        /// 格式化Bool
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static string Format(this bool b)
        {
            var str = "";
            if (b == true)
            {
                str = "✔";
            }
            else
            {
                str = "✘";
            }
            return str;
        }

        /// <summary>
        /// List>T<随机乱序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<T> ToRandomList<T>(this List<T> list)
        {
            Random random = new Random();
            List<T> newList = new List<T>();
            foreach (T item in list)
            {
                newList.Insert(random.Next(newList.Count), item);
            }
            return newList;
        }

        /// <summary>
        /// unix时间转换为datetime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime UnixTimeToTime(long timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            //TimeSpan toNow = new TimeSpan(timeStamp);
            return dtStart.Add(toNow);
        }

    }
}
